<?php

namespace app\blog\model;
use think\Model;
use think\Db;
class MemberModel extends Model{
    //会员列表查询
    public function indexlist($keywordComplex,$comeon){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.user_id'] = $adminid;
        }
        //判断会员所属店铺
        $data = Db::name("user")->whereOr($keywordComplex)->where($comeon)->alias("a")
                ->where($where)
                ->join('__SHOP__ b','b.re_guid=a.shop_guid','left')
                ->join('__MEMBER__ c','c.shop_id=b.re_guid')
                ->join('__MEMBERLEVEL__ d','d.ml_guid=c.pay_level','left')
                ->field('a.*,b.shop_name,c.id,c.m_guid,c.nick_name,c.avatar,c.mobile,c.address,c.status,c.c_time,c.money,c.m_integral,c.pay_level,d.name')
                ->paginate(10);
        return $data;
    }
    //添加店铺钱查询会员手机号是否存在
    public function ExistMember($membermobile){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $result = Db::name("user")->alias("a")
                ->where("a.id",$adminid)
                ->join('__MEMBER__ b','b.shop_id=a.shop_guid','left')
                ->where("b.mobile",$membermobile)
                ->field('a.*,b.mobile')
                ->find();
        return $result;
    }
    //添加会员，查询会员来源
    public function MemberSourceLog(){
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $shop_guid = Db::name("user")->where('id',$adminid)->find();
        $result = Db::name("source")->where("shop_guid",$shop_guid["shop_guid"])->select();
        return $result;
    }
    //添加会员前判断是否有店铺
    public function MemberShopfor(){
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $result = Db::name("user")->where("id",$adminid)->find();
        return $result["shop_guid"];
    }
    //添加会员数据
    public function addpo($post){
        //判断店铺
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $userguid = Db::name("user")->where("id",$adminid)->find();
        /*
         * 添加数据层
         * password_hash(值, PASSWORD_DEFAULT);哈希算法,default取值和php版本有关
         * password_verify($password, $hash)$password接收数据 $hash原数据，验证为true
         */
        $log = array();
        $log['m_guid'] = guid();//唯一标识
        $log['nick_name'] = "天使之吻";//默认昵称
        $log['avatar'] = "avatar/default/avatar.jpg";
        $log['mobile'] = $post['mobile'];
        $log['address'] = $post['address'];
        $log['status'] = $post['status'];
        $log['c_time'] = time();
        $log['pass_word'] = password_hash($post['pass_word'], PASSWORD_DEFAULT);
        $log['pay_word'] = password_hash($post['pay_word'], PASSWORD_DEFAULT);
        $log['shop_id'] = $userguid['shop_guid'];//店铺唯一标识
        $log['true_name'] = $post['true_name'];//真实姓名
        $log['sex'] = $post['sex'];
        $log['birthday'] = $post['birthday'];
        $log['comment'] = $post['comment'];
        $log['hobby'] = $post['hobby'];
        $log['job'] = $post['job'];
        $log['units'] = $post['units'];
        $log['improve'] = $post['improve'];
        $log['height'] = $post['height'];
        $log['marriage'] = $post['marriage'];
        $log['source_guid'] = $post['source_guid'];
        $result = Db::name("member")->insert($log);
        return $result;
    }
    //删除会员
    public function MemeberDelete($m_guid){
        $result = Db::name("member")->where("m_guid",$m_guid)->delete();
        return $result;
    }
    //获取会员历史记录
    public function MemberHistory($m_guid){
        $result = Db::name("member")->where("m_guid",$m_guid)->find();
        return $result;
    }
    //修改会员信息
    public function MemberUpdate($data){
        $log = array();
        $log['address'] = $data["address"];
        $log['pass_word'] = password_hash($data["pass_word"], PASSWORD_DEFAULT);
        $log['pay_word'] = password_hash($data["pay_word"], PASSWORD_DEFAULT);
        $log['true_name'] = $data['true_name'];
        $log['sex'] = $data['sex'];
        $log['comment'] = $data['comment'];
        $log['hobby'] = $data['hobby'];
        $log['job'] = $data['job'];
        $log['units'] = $data['units'];
        $log['improve'] = $data['improve'];
        $log['height'] = $data['height'];
        $log['marriage'] = $data['marriage'];
        $result = Db::name("member")->where("m_guid",$data["m_guid"])->update($log);
        return $result;
    }
    //查询会员余额变更记录
    public function MemberMoneyLog($m_guid,$comeon){
        $result = Db::name("memberbalog")->where($comeon)->where("member_id",$m_guid)->order("c_time DESC")->paginate(20);
        return $result;
    }
    //会员等级变更记录
    public function MemberPayleveLog($m_guid,$comeon){
        $result = Db::name("paylevelog")->where($comeon)->where("member_id",$m_guid)->order("c_time DESC")->paginate(20);
        return $result;
    }
    //会员积分变更记录
    public function MemberIntegraLog($m_guid,$comeon){
        $result = Db::name("integralog")->where($comeon)->where("member_id",$m_guid)->order("c_time DESC")->paginate(20);
        return $result;
    }
    //会员来源列表
    public function MemberSourceDate($keywordComplex){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.id'] = $adminid;
        }
        $result = Db::name("user")->alias("a")->where($keywordComplex)->where($where)
               ->join('__SOURCE__ b','b.shop_guid=a.shop_guid')
               ->join('__SHOP__ c','c.re_guid=a.shop_guid') 
               ->field('a.*,b.source_name,b.sou_guid,c.shop_name')->paginate(10);
        return $result;
    }
    //添加会员来源数据
    public function SourcePostdate($data){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $shop_guid = Db::name("user")->where("id",$adminid)->find();//查询店铺guid
        $log = array();
        $log["sou_guid"] = guid();
        $log["shop_guid"] = $shop_guid["shop_guid"];
        $log['source_name'] = $data['source_name'];
        $result = Db::name("source")->insert($log);
        return $result;
    }
    //删除会员来源数据
    public function SourceDeleteDate($m_guid){
        $result = Db::name("source")->where("sou_guid",$m_guid)->delete();
        return $result;
    }
    /*****************************会员卡********************************** */
    ////会员卡列表
    public function MemberMcard($keywordComplex,$comeon){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.id'] = $adminid;
        }
        //判断会员所属店铺
             $data = Db::name("user")->whereOr($keywordComplex)->where($comeon)->alias("a")->where($where)
                     //->join('__AGENCY__ b','b.user_id=a.user_guid','left')
                     ->join('__SHOP__ b','b.re_guid=a.shop_guid','left')
                     ->join('__MCARD__ c','c.shop_id=b.re_guid')
                     ->join('__CARD_CLASSIFY__ d','d.cfy_guid=c.classify_guid')
                     ->field('a.*,b.shop_name,c.cardname,c.card_type,c.card_is,c.status,a.mobile,c.c_time,c.cd_guid,c.id,c.classify_guid,c.valid_time,c.price,c.overdue_is,c.goods_dis,c.serve_dis,c.comment,d.cfy_name')
                     ->order("c.c_time DESC")
                     ->paginate(10);
             return $data;
    }
    //会员卡次卡分类列表
    public function CardClassifyList($keywordComplex){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $result = Db::name("user")->where($keywordComplex)->alias("a")
                ->where("a.id",$adminid)
                ->join('__SHOP__ b','b.re_guid=a.shop_guid','left')
                ->join('__CARD_CLASSIFY__ c','c.shop_guid=b.re_guid','left')
                ->field('a.*,b.shop_name,c.cfy_name,c.cfy_guid,a.mobile')
                ->paginate(15);
        return $result;
    }
    //接收会员次卡分类页面数据
    public function CardClassifygop($data){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $shop_guid = Db::name("user")->where("id",$adminid)->find();
        $log = array();
        $log['cfy_guid'] = guid();
        $log['cfy_name'] = $data['cfy_name'];
        $log['shop_guid'] = $shop_guid["shop_guid"];
        $result = Db::name("card_classify")->insert($log);
        return $result;
    }
    //删除会员卡分类数据
    public function CardClassifygoDel($m_guid){
        $result = Db::name("card_classify")->where("cfy_guid",$m_guid)->delete();
        return $result;
    }
    //判断会员卡状态 冻结
    public function MemberCardban($id){
        $result = Db::name("mcard")->where("cd_guid",$id)->setField('status', '2');
        return $result;
    }
    //判断会员卡状态 恢复
    public function MemberCardcancelBan($id){
        $result = Db::name('mcard')->where("cd_guid",$id)->setField('status', '1');
        return $result;
    }
    //添加会员次卡之前，查询次卡分类
    public function MemberCardClassify(){
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $shop_guid = Db::name("user")->where('id',$adminid)->find();
        $result = Db::name("card_classify")->where("shop_guid",$shop_guid["shop_guid"])->select();
        return $result;
    }
    //添加会员卡
    public function MemberCardPost($data){
        //判断店铺
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $userguid = Db::name("user")->where("id",$adminid)->find();
        $shopid = Db::name("agency")->where("user_id=".$userguid["user_guid"])->alias("a")
                  ->join('__SHOP__ b','b.agency_id=a.ay_guid','left')
                  ->field('a.*,b.re_guid')->find();
        $log = array();
        $log['cd_guid'] = guid();//唯一标识
        $log['shop_id'] = $shopid['re_guid'];//店铺id
        $log['card_type'] = $data['card_type'];
        $log['card_is'] = $data['card_is'];
        $log['cardname'] = $data['cardname'];
        $log['status'] = $data['status'];
        $log['c_time'] = time();
        $log['valid_time'] = $data['valid_time'];
        $log['price'] = $data['price'];
        $log['overdue_is'] = $data['overdue_is'];
        $log['goods_dis'] = $data['goods_dis'];
        $log['serve_dis'] = $data['serve_dis'];
        $log['comment'] = $data['post_content'];
        $log['classify_guid'] = $data["classify_guid"];
        $result = Db::name("mcard")->insert($log);
        return $result;
    }
    //获取会员卡历史记录
    public function MemberCardHistory($m_guid){
        $result = Db::name("mcard")->where("cd_guid",$m_guid)->find();
        return $result;
    }
    //获取会员卡提交数据
    public function MemberCardUpdatePost($data){
        $log = array();
        $log['cardname'] = $data["cardname"];
        $log['card_type'] = $data["card_type"];
        $log['card_is'] = $data["card_is"];
        $log['status'] = $data["status"];
        $log['valid_time'] = $data['valid_time'];
        $log['price'] = (int)$data['price'];
        $log['classify_guid'] = $data['classify_guid'];
        $log['overdue_is'] = $data['overdue_is'];
        $log['goods_dis'] = $data['goods_dis'];
        $log['serve_dis'] = $data['serve_dis'];
        $log['comment'] = $data['post_content'];
        $result = Db::name("mcard")->where("cd_guid",$data['m_guid'])->update($log);
        return $result;
    }
    //查询会员拥有的会员卡
    public function MemberOwnmebCard($m_guid){
        $result = Db::name("membercard")->where("member_id",$m_guid)->find();
        $data = Db::name("mcard")->where("cd_guid",'in',$result["card_id"])
                ->field("cardname,card_type,card_is,status,c_time")->paginate(20);
        return $data;
    }
    //会员充值记录查询
    public function MemberChangeLoging($keywordComplex,$comeon){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.user_id'] = $adminid;
        }
        //判断会员所属店铺
        $data = Db::name("agency")->whereOr($keywordComplex)->where($comeon)->alias("a")
                ->where($where)
                ->join('__SHOP__ b','b.agency_id=a.ay_guid','left')
                ->join('__MEMBER__ c','c.shop_id=b.re_guid')
                ->join('__MEMBERCHANGE__ d','d.member_id=c.m_guid')
                ->field('a.*,b.shop_name,c.nick_name,c.mobile,d.money,d.order_no,d.status,d.pay_way,d.content,d.c_time')
                ->paginate(10);
        return $data;
    }
    //会员储值卡列表
    public function MemberStoreValue($keywordComplex,$comeon){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.id'] = $adminid;
        }
        $data = Db::name("user")->whereOr($keywordComplex)->where($comeon)->alias("a")->where($where)
               ->join('__SHOP__ b','b.re_guid=a.shop_guid','left') 
               ->join('__STORED_VALUE__ c','c.shop_id=b.re_guid')
               ->field('a.*,b.shop_name,c.sv_guid,c.stov_name,c.sv_price,c.zs_price,c.sj_price,c.overdue_is,c.svcard_is,c.goods_dis,c.serve_dis,c.status,c.c_time,c.valid_time,c.use_time,c.comment')
               ->paginate(10);
        return $data;
    }
    //判断储值卡状态 冻结
    public function MemberStoreban($id){
        $result = Db::name("stored_value")->where("sv_guid",$id)->setField('status', '2');
        return $result;
    }
    //判断储值卡状态 恢复
    public function MemberStorecancelBan($id){
        $result = Db::name('stored_value')->where("sv_guid",$id)->setField('status', '1');
        return $result;
    }
    //储值卡提交添加数据
    public function MemberStoredPostDa($data){
        //判断店铺
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $shopid = Db::name("user")->where("id",$adminid)->find();//查询店铺id
        $log = array();
        $log["sv_guid"] = guid();//获取唯一标识
        $log["shop_id"] = $shopid["shop_guid"];
        $log["stov_name"] = $data["stov_name"];
        $log["sv_price"] = $data["sv_price"];
        $log["zs_price"] = $data["zs_price"];
        $log["sj_price"] = $data["sj_price"];
        $log["overdue_is"] = $data["overdue_is"];
        $log["svcard_is"] = $data["svcard_is"];
        $log["goods_dis"] = $data["goods_dis"];
        $log["serve_dis"] = $data["serve_dis"];
        $log["status"] = $data["status"];
        $log["c_time"] = time();
        $log["valid_time"] = $data["valid_time"];
        $log["use_time"] = 0;
        $log["comment"] = $data["post_content"];
        $result = Db::name("stored_value")->insert($log);
        return $result;
    }
    //获取储值卡历史记录
    public function MemberStoredHistory($m_guid){
        $result = Db::name("stored_value")->where("sv_guid",$m_guid)->find();
        return $result;
    }
    //获取储值卡数据修改
    public function MemberStoredUpdateDa($data){
        $log = array();
        $log["stov_name"] = $data["stov_name"];
        $log["sv_price"] = (int)$data["sv_price"];
        $log["zs_price"] = (int)$data["zs_price"];
        $log["sj_price"] = (int)$data["sj_price"];
        $log["valid_time"] = $data["valid_time"];
        $log["overdue_is"] = $data["overdue_is"];
        $log["svcard_is"] = $data["svcard_is"];
        $log["goods_dis"] = $data["goods_dis"];
        $log["serve_dis"] = $data["serve_dis"];
        $log["status"] = $data["status"];
        $log["comment"] = $data["post_content"];
        $result = Db::name("stored_value")->where("sv_guid",$data['m_guid'])->update($log);
        return $result;
    }
    /**************************会员等级****************************************/
    public function MemberLevelc($keywordComplex,$comeon){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.id'] = $adminid;
        }
        //判断会员所属店铺
        $data = Db::name("user")->whereOr($keywordComplex)->where($comeon)->alias("a")->where($where)
                ->join('__AGENCY__ b','b.user_id=a.user_guid','left')
                ->join('__SHOP__ c','c.agency_id=b.ay_guid','left')
                ->join('__MEMBERLEVEL__ d','d.shop_id=c.re_guid')
                ->field('a.*,c.shop_name,a.mobile,d.id,d.name,d.money,d.c_time,d.dis_count,d.status,d.ml_guid')
                ->paginate(10);
             return $data;
    }
    //判断会员等级状态 冻结
    public function MemberLevelban($id){
        $result = Db::name("memberlevel")->where("ml_guid",$id)->setField('status', '2');
        return $result;
    }
    //判断会员等级状态 恢复
    public function MemberLevelcancelBan($id){
        $result = Db::name('memberlevel')->where("ml_guid",$id)->setField('status', '1');
        return $result;
    }
    //接收会员等级添加数据
    public function MemberLevelPost($data){
        //判断店铺
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $userguid = Db::name("user")->where("id",$adminid)->find();
        $shopid = Db::name("agency")->where("user_id=".$userguid["user_guid"])->alias("a")
                  ->join('__SHOP__ b','b.agency_id=a.ay_guid','left')
                  ->field('a.*,b.re_guid')->find();
        $log = array();
        $log["ml_guid"] = guid();//唯一标识
        $log["name"] = $data["name"];
        $log["money"] = $data['money'];
        $log['shop_id'] = $shopid["re_guid"];
        $log["dis_count"] = $data["dis_count"];
        $log["status"] = $data['status'];
        $log["c_time"] = time();
        $result = Db::name("memberlevel")->insert($log);
        return $result;
    }
    //会员等级历史记录
    public function MemberLevelHistory($m_guid){
        $result = Db::name("memberlevel")->where("ml_guid",$m_guid)->find();
        return $result;
    }
    //修改会员等级提交数据
    public function MemberLevelUpdate($data){
        $log = array();
        $log['name'] = $data['name'];
        $log["money"] = $data['money'];
        $log['dis_count'] = $data['dis_count'];
        $log['status'] = $data['status'];
        $result = Db::name("memberlevel")->where("ml_guid",$data['m_guid'])->update($log);
        return $result;
    }
    //删除会员等级
    public function MemberLevelDelete($m_guid){
        $result = Db::name("memberlevel")->where("ml_guid",$m_guid)->delete();
        return $result;
    }
    /****************************预约功能设置***************************************/
    //预约管理列表数据
    public function MemberAppointment($keywordComplex){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.id'] = $adminid;
        }
        //判断会员所属店铺
        $data = Db::name("user")->where($keywordComplex)->alias("a")->where($where)
                ->join('__AGENCY__ b','b.user_id=a.user_guid','left')
                ->join('__SHOP__ c','c.agency_id=b.ay_guid','left')
                ->join('__APPOINTMENT__ d','d.shop_guid=c.re_guid')
                ->field('a.*,c.shop_name,a.mobile,d.apm_guid,d.money,d.back,d.start_content,d.end_content')
                ->paginate(10);
        return $data;
    }
    //判断预约状态 关闭
    public function MemberAppoinmentban($id){
        $result = Db::name("appointment")->where("apm_guid",$id)->setField('back', '2');
        return $result;
    }
    //判断预约状态 开启
    public function MemberAppoinmentCarban($id){
        $result = Db::name("appointment")->where("apm_guid",$id)->setField('back', '1');
        return $result;
    }
    //会员预约添加数据
    public function MemberAppoPostAdd($data){
        //判断店铺
        $adminid = cmf_get_current_admin_id();//查询当前登录的userid
        $userguid = Db::name("user")->where("id",$adminid)->find();
        $shopid = Db::name("agency")->where("user_id=".$userguid["user_guid"])->alias("a")
                  ->join('__SHOP__ b','b.re_guid=a.ay_guid','left')
                  ->field('a.*,b.re_guid')->find();
        $log = array();
        $log["apm_guid"] = guid();//唯一标识
        $log["shop_guid"] = $shopid["re_guid"];
        $log["money"] = $data["money"];
        $log["back"] = $data["back"];
        $log["start_content"] = $data["start_content"];
        $log["end_content"] = $data["end_content"];
        $result = Db::name("appointment")->insert($log);
        return $result;
    }
    //修改预约信息
    public function MemberAppoinmentHistory($m_guid){
        $result = Db::name("appointment")->where("apm_guid",$m_guid)->find();
        return $result;
    }
    //修改预约旧数据
    public function MemberAppoinmentUpdatePost($data){
        $log = array();
        $log['money'] = $data["money"];
        $log['back'] = $data["back"];
        $log['start_content'] = $data["start_content"];
        $log['end_content'] = $data["end_content"];
        $result = Db::name("appointment")->where("apm_guid",$data['m_guid'])->update($log);
        return $result;
    }
    //删除预约设置
    public function AppointmentDelete($m_guid){
        $result = Db::name("appointment")->where("apm_guid",$m_guid)->delete();
        return $result;
    }
    //查询会员预约记录数据
    public function AppointmentLog($keywordComplex,$comeon){
        //查询登录id
        $adminid = cmf_get_current_admin_id();
        $where = array();
        if($adminid != 1){
             $where['a.id'] = $adminid;
        }
        //判断会员所属店铺
        $data = Db::name("user")->whereOr($keywordComplex)->where($comeon)->alias("a")->where($where)
                ->join('__AGENCY__ b','b.user_id=a.user_guid','left')
                ->join('__SHOP__ c','c.agency_id=b.ay_guid','left')
                 ->join('__APPOINTMENTORDER__ d','d.shop_guid=c.re_guid')
                ->join('__MEMBER__ e','e.m_guid=d.member_guid','left')
                ->join('__GOODS__ f','f.goods_guid=d.product_id','left')
                ->field('a.*,c.shop_name,d.order_id,d.app_time,d.appone_time,d.end_time,d.money,d.status,d.for_status,f.goods_name,e.nick_name,e.mobile,d.ap_guid')
                ->paginate(20);
        return $data;
    }
    //判断预约记录是否来店 未到
    public function MemberAppoinmentLogban($id){
        $result = Db::name("appointmentorder")->where("ap_guid",$id)->setField('for_status', '2');
        return $result;
    }
    //判断预约记录是否来店 已来店
    public function AppoCarLogban($id){
        $result = Db::name("appointmentorder")->where("ap_guid",$id)->setField('for_status', '1');
        return $result;
    }
}

